package com.ruslan.magaz.domain;
// default package
// Generated 28.09.2014 13:25:18 by Hibernate Tools 3.4.0.CR1


import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import com.ruslan.magaz.web.EmailExistsConstraint;
import com.ruslan.magaz.web.UserExistsConstraint;

/**
 * User generated by hbm2java
 */
@Entity
@Table(name = "user", catalog = "eshop")
//@UserExistsConstraint
//@EmailExistsConstraint
public class User implements java.io.Serializable {


	private int userId;
	private Role role;
	@Size(min=3,max=20,message=	"Username must be between 3 and 20 characters long.")
	@Pattern(regexp="^[a-zA-Z0-9]+$",	message="Username must be latin letters and numbers only")
	private String name;
	@Size(min=3,max=20,message=	"Password must be between 3 and 20 characters long.")
	@Pattern(regexp="^[a-zA-Z0-9]+$",	message="Password must be be latin letters and numbers only")
	private String password;
	private String retypePassword;
	@Size(min=3,max=30,message=	"email is not valid")
	@Pattern(regexp="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$",	message="email is not valid")
	private String email;
	private Set<Rating> ratings = new HashSet<Rating>(0);
	private Customer customer;

	public User() {
	}

	public User(int userId) {
		this.userId = userId;
	}
	public User(Role role) {
//		this.userId = userId;
		this.role = role;
	}
	
	public User(Role role, String name, String password,
			String email) {
//		this.userId = userId;
		this.role = role;
		this.name = name;
		this.password = password;
		this.email = email;
	}



	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "User_id", unique = true, nullable = false)
	public int getUserId() {
		return this.userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "Role_id", nullable = false)
	public Role getRole() {
		return this.role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	@Column(name = "Name",unique = true, length = 100)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "Password", length = 100)
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "Email", unique = true, length = 100)
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}


	@OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade=CascadeType.REMOVE)
//	@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
	public Set<Rating> getRatings() {
		return this.ratings;
	}

	public void setRatings(Set<Rating> ratings) {
		this.ratings = ratings;
	}

	@OneToOne(fetch = FetchType.EAGER, mappedBy = "user", cascade= CascadeType.ALL)
//	@OneToOne(fetch = FetchType.EAGER, mappedBy = "user")
	public Customer getCustomer() {
		return this.customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
	@Transient
	public String getRetypePassword() {
		return retypePassword;
	}

	public void setRetypePassword(String retypePassword) {
		this.retypePassword = retypePassword;
	}


}
